﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Infragistics.Composite.Wpf.Proxies.Docking;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Infragistics.Composite.Wpf.Proxies.Tests.Docking
{
    /// <summary>
    /// Unit tests for the ContentPaneProxy class.
    /// </summary>
    [TestClass]
    public class ContentPaneProxyTests
    {
        #region Boilerplate Code

        public ContentPaneProxyTests()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #endregion // Boilerplate Code

        const string CONTENT_VALUE = "test data";

        [TestMethod]
        public void TestContentPane()
        {
            ContentPaneProxy target = new ContentPaneProxy(CONTENT_VALUE);

            Assert.IsNotNull(target.ContentPane);
            Assert.AreEqual(CONTENT_VALUE, target.ContentPane.Content);
            Assert.AreEqual(target, ContentPaneProxy.GetAssociatedProxy(target.ContentPane));
        }

        [TestMethod]
        public void TestLocalPropertyValueIsIgnored()
        {
            ContentPaneProxy target = new ContentPaneProxy(CONTENT_VALUE);
            Assert.IsTrue(target.ContentPane.AllowClose);

            target.AllowClose = false;

            Assert.IsTrue(target.ContentPane.AllowClose,
                "The ContentPane should not be modified due to a local property setting on the proxy.  " + 
                "Only a Style applied to the ContentPaneProxy should affect the underlying ContentPane's properties.");
        }

        [TestMethod]
        public void TestProxiedProperties()
        {
            ContentPaneProxy target = new ContentPaneProxy(CONTENT_VALUE);

            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.AllowCloseProperty, false), "AllowClose");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.AllowDockingProperty, false), "AllowDocking");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.AllowDockingBottomProperty, false), "AllowDockingBottom");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.AllowDockingFloatingProperty, false), "AllowDockingFloating");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.AllowDockingInTabGroupProperty, false), "AllowDockingInTabGroup");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.AllowDockingLeftProperty, false), "AllowDockingLeft");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.AllowDockingRightProperty, false), "AllowDockingRight");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.AllowDockingTopProperty, false), "AllowDockingTop");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.AllowFloatingOnlyProperty, true), "AllowFloatingOnly");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.AllowInDocumentHostProperty, false), "AllowInDocumentHost");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.AllowPinningProperty, false), "AllowPinning");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.IsPinnedProperty, true), "IsPinned");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.SaveInLayoutProperty, false), "SaveInLayout");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.SerializationIdProperty, "my_id"), "SerializationId");

            BitmapImage bmp = new BitmapImage(new Uri(@"..\image.png", UriKind.Relative));
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.ImageProperty, bmp), "Image");

            DataTemplate dataTemplate = new DataTemplate();
            DataTemplateSelector dataTemplateSelector = new DataTemplateSelector();

            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.NavigatorDescriptionProperty, CONTENT_VALUE), "NavigatorDescription");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.NavigatorDescriptionTemplateProperty, dataTemplate), "NavigatorDescriptionTemplate");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.NavigatorDescriptionTemplateSelectorProperty, dataTemplateSelector), "NavigatorDescriptionTemplateSelector");

            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.NavigatorTitleProperty, CONTENT_VALUE), "NavigatorTitle");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.NavigatorTitleTemplateProperty, dataTemplate), "NavigatorTitleTemplate");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.NavigatorTitleTemplateSelectorProperty, dataTemplateSelector), "NavigatorTitleTemplateSelector");

            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.TabHeaderProperty, CONTENT_VALUE), "TabHeader");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.TabHeaderTemplateProperty, dataTemplate), "TabHeaderTemplate");
            Assert.IsTrue(VerifyPropertyValue(target, ContentPaneProxy.TabHeaderTemplateSelectorProperty, dataTemplateSelector), "TabHeaderTemplateSelector");
        }

        bool VerifyPropertyValue(ContentPaneProxy target, DependencyProperty property, object value)
        {           
            Style style = new Style(typeof(ContentPaneProxy));
            style.Setters.Add(new Setter(property, value));
            target.Style = style;

            object actualValue = target.ContentPane.GetValue(property);
            return Object.Equals(value, actualValue);
        }
    }
}